package hz.mobileproject.pda.ui.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.entity.response.Response_familyPhone_item;
import hz.mobileproject.pda.network.Constance;

/**
 * Created by Lc on 2017/10/18.
 */

public class FamilyPhoneAdapter extends RecyclerView.Adapter<FamilyPhoneAdapter.MyViewHolder> {

    private Context mContext;
    private List<Response_familyPhone_item> listData;
    private  OnItemClickListener onItemClickListener;
    //上一个播放的position
    private int lastPlayPosition = -1;

    public FamilyPhoneAdapter(Context c,List<Response_familyPhone_item> listData) {
        this.mContext = c;
        this.listData = listData;
        if(this.listData == null){
            this.listData = new ArrayList<Response_familyPhone_item>();
        }
    }

    public List<Response_familyPhone_item> getListData() {
        return listData;
    }

    public void setListData(List<Response_familyPhone_item> listData) {
        if(listData == null){
            listData = new ArrayList<Response_familyPhone_item>();
        }
        this.listData.clear();
        this.listData.addAll(listData);
        this.notifyDataSetChanged();
    }
    public void addListData(List<Response_familyPhone_item> listData){
        if(listData == null){
            return;
        }
        this.listData.addAll(listData);
        this.notifyDataSetChanged();
    }
    public void deleteOne(int position){
        if(listData == null || position >= listData.size()){
            return;
        }
        this.listData.remove(position);
        this.notifyDataSetChanged();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(
                LayoutInflater.from(mContext).inflate(R.layout.layout_family_phone_incv, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        if(position == 0){
            holder._v_line.setVisibility(View.VISIBLE);
        }else{
            holder._v_line.setVisibility(View.GONE);
        }
        holder._pr_name.setText(listData.get(position).getPr_name());
        holder._pr_no.setText(listData.get(position).getPr_no());
        holder._area_name.setText(listData.get(position).getArea_name());
        holder._area_sub_name.setText(listData.get(position).getArea_sub_name());
        holder._person_name.setText(listData.get(position).getPerson_name());
        holder._person_relation.setText(listData.get(position).getPerson_relation());
        holder._phone_numver.setText(listData.get(position).getPhone_numver());
        holder._start_time.setText(listData.get(position).getStart_time());
        holder._end_time.setText(listData.get(position).getEnd_time());
        holder._time_long.setText(listData.get(position).getTime_long()+"秒");
        holder._ll_root_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.OnItemClick(position,listData.get(position));
                }
            }
        });
        //谈话录音评估
        if(StringUtils.isEmpty(listData.get(position).getTalk_effect_name())){
            holder.tv_remark.setText("");
            holder.tv_remark_left.setVisibility(View.GONE);
            holder.tv_remark.setVisibility(View.GONE);
        }else{
            holder.tv_remark.setText(listData.get(position).getTalk_effect_name());
            holder.tv_remark_left.setVisibility(View.VISIBLE);
            holder.tv_remark.setVisibility(View.VISIBLE);
            if(Constance.TALK_EFFECT_OK.equals(listData.get(position).getTalk_effect())){
                holder.tv_remark.setTextColor(ContextCompat.getColor(mContext,R.color.green));
            }else{
                holder.tv_remark.setTextColor(ContextCompat.getColor(mContext,R.color.orangered));
            }
        }
        //情绪评估
        if(StringUtils.isEmpty(listData.get(position).getEmotion_evaluate_name())){
            holder.tv_emotion.setText("");
            holder.tv_emotion_left.setVisibility(View.GONE);
            holder.tv_emotion.setVisibility(View.GONE);
        }else{
            holder.tv_emotion.setText(listData.get(position).getEmotion_evaluate_name());
            holder.tv_emotion_left.setVisibility(View.VISIBLE);
            holder.tv_emotion.setVisibility(View.VISIBLE);
            if(Constance.EMOTION_EVALUATE_OK.equals(listData.get(position).getEmotion_evaluate())){
                holder.tv_emotion.setTextColor(ContextCompat.getColor(mContext,R.color.green));
            }else{
                holder.tv_emotion.setTextColor(ContextCompat.getColor(mContext,R.color.orangered));
            }
        }
        //危险评估
        if(StringUtils.isEmpty(listData.get(position).getDanger_evaluate_name())){
            holder.tv_danger.setText("");
            holder.tv_danger_left.setVisibility(View.GONE);
            holder.tv_danger.setVisibility(View.GONE);
        }else{
            holder.tv_danger.setText(listData.get(position).getDanger_evaluate_name());
            holder.tv_danger_left.setVisibility(View.VISIBLE);
            holder.tv_danger.setVisibility(View.VISIBLE);
            if(Constance.DANGER_EVALUATE_OK.equals(listData.get(position).getDanger_evaluate())){
                holder.tv_danger.setTextColor(ContextCompat.getColor(mContext,R.color.green));
            }else{
                holder.tv_danger.setTextColor(ContextCompat.getColor(mContext,R.color.orangered));
            }
        }

    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

    public void clearAll() {
        this.listData.clear();
        this.notifyDataSetChanged();
    }


    class MyViewHolder extends RecyclerView.ViewHolder
    {
        private TextView _pr_no;
        private TextView _pr_name;
        private TextView _area_name;
        private TextView _area_sub_name;
        private TextView _person_name;
        private TextView _person_relation;
        private TextView _phone_numver;
        private TextView _start_time;
        private TextView _end_time;
        private TextView _time_long;
        private View _v_line;
        private LinearLayout _ll_root_view;
        private TextView tv_remark;
        private TextView tv_remark_left;
        private TextView tv_emotion;
        private TextView tv_emotion_left;
        private TextView tv_danger;
        private TextView tv_danger_left;


        public MyViewHolder(View view)
        {
            super(view);
            _pr_no = (TextView)view.findViewById(R.id.tv_pr_no);
            _pr_name = (TextView)view.findViewById(R.id.tv_pr_name);
            _area_name = (TextView)view.findViewById(R.id.tv_area_name);
            _area_sub_name = (TextView)view.findViewById(R.id.tv_area_sub_name);
            _person_name = (TextView)view.findViewById(R.id.tv_person_name);
            _person_relation = (TextView)view.findViewById(R.id.tv_person_relation);
            _phone_numver = (TextView)view.findViewById(R.id.tv_phone_numver);
            _start_time = (TextView)view.findViewById(R.id.tv_start_time);
            _end_time = (TextView)view.findViewById(R.id.tv_end_time);
            _time_long = (TextView)view.findViewById(R.id.tv_time_long);
            _v_line = view.findViewById(R.id.v_line);
            _ll_root_view = (LinearLayout) view.findViewById(R.id.ll_rootView);
            tv_remark = (TextView) view.findViewById(R.id.tv_remark);
            tv_remark_left = (TextView) view.findViewById(R.id.tv_remark_left);
            tv_emotion = (TextView) view.findViewById(R.id.tv_emotion);
            tv_emotion_left = (TextView) view.findViewById(R.id.tv_emotion_left);
            tv_danger = (TextView) view.findViewById(R.id.tv_danger);
            tv_danger_left = (TextView) view.findViewById(R.id.tv_danger_left);
        }
    }

    public interface OnItemClickListener{
        void OnItemClick(int position, Response_familyPhone_item item);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
